//
//  TopicViewController.m
//  Slice
//
//  Created by lanou on 16/6/15.
//  Copyright © 2016年 wangjinchao. All rights reserved.
//

#import "TopicViewController.h"

@interface TopicViewController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation TopicViewController
- (NSMutableArray *)dataArray {
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void) getData {
    [RequestTool requsetWithType:POST URLString:kTopicListURl paremeter:@{} callBack:^(NSData *data, NSError *error) {
        if (data == nil) {
            return ;
        }
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *dic in dataDic[@"data"][@"list"]) {

            [self.dataArray addObject:[TopicLIstModel modelWithDiction:dic]];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        }); 
    }];
}




- (void)viewDidLoad {
    
    NSLog(@"闹着玩呢");
    
   
    NSLog(@"干你妹");
    
    
    [super viewDidLoad];
    self.navigationItem.title = @"话题";
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self getData];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, kWidth, kHeight - 65) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"TopicListTableViewCell" bundle:nil] forCellReuseIdentifier:@"123"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TopicList22TableViewCell" bundle:nil] forCellReuseIdentifier:@"456"];
}

//// segControl 的动作方法
//- (void)segClick:(UISegmentedControl *)seg {
//    switch (seg.selectedSegmentIndex) {
//        case 0: {
//            
//            break;
//        }
//        default: {
//            break;
//        }
//    }
//}

#pragma mark -代理方法-
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    TopicLIstModel *model = self.dataArray[indexPath.row];
    CGFloat height = [self getHeightWithString:model.title];
    
    return height + 150;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TopicLIstModel *model = self.dataArray[indexPath.row];
    if (model.coverimg.length == 0) {
        TopicList22TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"456" forIndexPath:indexPath];
        [cell getDataByModel:self.dataArray[indexPath.row]];
        return cell;
    } else {

    TopicListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"123" forIndexPath:indexPath];
    [cell getDataByModel:self.dataArray[indexPath.row]];
    
    return cell;
    }
}

- (CGFloat) getHeightWithString:(NSString *) string {
    
    NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17], NSFontAttributeName, nil];
    
    CGRect rect = [string boundingRectWithSize:(CGSizeMake(kWidth - 100, 1000)) options:NSStringDrawingUsesLineFragmentOrigin attributes:dictionary context:nil];
    
    return rect.size.height;

}

//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    TpicDetailViewController *vc = [[TpicDetailViewController alloc] init];
//    TopicLIstModel *model = self.dataArray[indexPath.row];
//    vc.contentid = model.contentid;
//    [self.navigationController pushViewController:vc animated:YES];
//}





@end
